package com.game.module.${moduleName?lower_case}.structs;

import com.game.module.player.component.PlayerBean;
import common.persistence.annotation.DataConverter;
import common.persistence.annotation.TableName;
import com.game.module.${moduleName?lower_case}.*;
import common.utils.buffer.BufferWrapper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.List;

import common.utils.buffer.BufferWrapper;
import common.utils.buffer.ByteSerialize;

/**
* @date ${.now?string("yyyy-MM-dd HH:mm:ss")}
*/
public class ${assistEntity.entityName} implements ByteSerialize, ProtocolConvertible<${assistEntity.entityName}Dto>{

    private final static int VERSION_1 = 1;
    private final static int CUR_VERSION = VERSION_1;

	<#-- filed -->
	<#if assistEntity.entityBeans ? exists>
	<#list assistEntity.entityBeans as bean>
	/**
	* ${bean.desc}
	*/
	private ${bean.type} ${bean.field};
	</#list>
	</#if>
	
	<#-- Constructor -->
	public ${assistEntity.entityName}(){
		<#if assistEntity.entityBeans ? exists>
		<#list assistEntity.entityBeans as bean>
		<#if bean.type == 'String'>
		this.${bean.field} = "";
		<#elseif bean.type ? starts_with('Map')>
		this.${bean.field} = new HashMap<>();
		<#elseif bean.type ? starts_with('List')>
		this.${bean.field} = new ArrayList<>();
		</#if>
		</#list>
		</#if>
	}
	
	<#-- Getter Setter-->
	<#if assistEntity.entityBeans ? exists>
	<#list assistEntity.entityBeans as bean>
	/** ${bean.desc} **/
	public ${bean.type} get${bean.field ? cap_first}(){
		return this.${bean.field};
	}
	
	public void set${bean.field ? cap_first}(${bean.type} ${bean.field}){
		this.${bean.field} = ${bean.field};
	}
	</#list>
	</#if>
	
	@Override
    public void readBuffer(BufferWrapper buffer) {
		int version = buffer.readInt();
        if (version == VERSION_1) {
            readBuffer1(buffer);
        } else {
            readBuffer1(buffer);
        }
    }
    
    public void readBuffer1(BufferWrapper buffer) {
    	<#if assistEntity.entityBeans ? exists>
		<#list assistEntity.entityBeans as bean>
		<#if bean.type == 'String'>
		this.${bean.field} = "";
		<#elseif bean.type == 'int' || bean.type == "Integer">
		this.${bean.field} = buffer.readInt();
		<#elseif bean.type == 'boolean' || bean.type == "Boolean">
		this.${bean.field} = buffer.readBoolean();
		<#elseif bean.type == 'double' || bean.type == "Double">
		this.${bean.field} = buffer.readDouble();
		<#elseif bean.type == 'float' || bean.type == "Float">
		this.${bean.field} = buffer.readFloat;
		<#elseif bean.type == 'long' || bean.type == "Long">
		this.${bean.field} = buffer.readLong();
		<#elseif bean.type == 'short' || bean.type == "Short">
		this.${bean.field} = buffer.readShort();
		<#elseif bean.type ? starts_with('List<Long>')>
		this.${bean.field} = buffer.readLongs();
		<#elseif bean.type ? starts_with('List<Integer>')>
		this.${bean.field} = buffer.readInts();
		<#elseif bean.type ? starts_with('List<String>')>
		this.${bean.field} = buffer.readStrings();
		<#elseif bean.type ? starts_with('List<')>
		<#-- 复杂类型的List-->
		this.${bean.field}.addAll(buffer.readDatas(${bean.genericType}.class));
		<#elseif bean.type ? starts_with('Map<Integer, Integer>')>
		this.${bean.field} = buffer.readIIMap();
		<#elseif bean.type ? starts_with('Map<Integer, Long>')>
		this.${bean.field} = buffer.readILMap();
		<#elseif bean.type ? starts_with('Map<Long, Integer>')>
		this.${bean.field} = buffer.readLIMap();
		<#elseif bean.type ? starts_with('Map<Long, Long>')>
		this.${bean.field} = buffer.readLLMap();
		<#elseif bean.type ? starts_with('Map<')>
		<#-- 复杂类型的Map-->
		Collection<${bean.genericType}> maps = buffer.readDatas(${bean.genericType}.class);
		maps.forEach(m -> this.${bean.field}.put(m.getIIIIIId(), m));
		<#else>
		this.${bean.field} = buffer.readUnSupportObject();
		</#if>
		</#list>
		</#if>
    }

    @Override
    public void writeBuffer(BufferWrapper buffer) {
    	writeBuffer1(buffer);
    }
    
    public void writeBuffer1(BufferWrapper buffer) {
    	buffer.writeInt(CUR_VERSION);
    	<#if assistEntity.entityBeans ? exists>
		<#list assistEntity.entityBeans as bean>
		<#if bean.type == 'String'>
		buffer.writeString(this.${bean.field});
		<#elseif bean.type == 'int' || bean.type == "Integer">
		buffer.writeInt(this.${bean.field});
		<#elseif bean.type == 'boolean' || bean.type == "Boolean">
		buffer.writeBoolean(this.${bean.field});
		<#elseif bean.type == 'double' || bean.type == "Double">
		buffer.writeDouble(this.${bean.field});
		<#elseif bean.type == 'float' || bean.type == "Float">
		buffer.writeFloat(this.${bean.field});
		<#elseif bean.type == 'long' || bean.type == "Long">
		buffer.writeLong(this.${bean.field});
		<#elseif bean.type == 'short' || bean.type == "Short">
		buffer.writeShort(this.${bean.field});
		<#elseif bean.type ? starts_with('List<Long>')>
		buffer.writeLongs(this.${bean.field});
		<#elseif bean.type ? starts_with('List<Integer>')>
		buffer.writeInts(this.${bean.field});
		<#elseif bean.type ? starts_with('List<String>')>
		buffer.writeStrings(this.${bean.field});
		<#elseif bean.type ? starts_with('List<')>
		<#-- 复杂类型的List-->
		buffer.writeDatas(this.${bean.field});
		<#elseif bean.type ? starts_with('Map<Integer, Integer>')>
		buffer.writeIIMap(this.${bean.field});
		<#elseif bean.type ? starts_with('Map<Integer, Long>')>
		buffer.writeILMap(this.${bean.field});
		<#elseif bean.type ? starts_with('Map<Long, Integer>')>
		buffer.writeLIMap(this.${bean.field});
		<#elseif bean.type ? starts_with('Map<Long, Long>')>
		buffer.writeLLMap(this.${bean.field});
		<#elseif bean.type ? starts_with('Map<')>
		<#-- 复杂类型的Map-->
		buffer.writeDatas(this.${bean.field}.values());
		<#else>
		buffer.writeUnSupportObject(this.${bean.field});
		</#if>
		</#list>
		</#if>
    }
	
	@Override
	public ${assistEntity.entityName}Dto buildProtocol() {
		${assistEntity.entityName}Dto dto = new ${assistEntity.entityName}Dto();
		<#if assistEntity.entityBeans ? exists>
		<#list assistEntity.entityBeans as bean>
		dto.set${bean.field ? cap_first}(${bean.field});
		</#list>
		</#if>
		return dto;
	}
}
